#!/usr/bin/ruby
$:.unshift File.join(File.dirname(__FILE__))
require 'AWS/SQS'
require 'basic_configuration'
require 'aws_context'
require 'pp'

#  Modify the given queue attributes.
#  For now, the only one is the visibility timeout.

if ARGV[0] == '-?' || ARGV.size != 2 then
  puts "usage: modify-queue name visibility-timeout"
  exit 1
end
name=ARGV[0]
visibility_timeout=ARGV[1].to_i

if visibility_timeout <= 0 then
  puts "visibility-timeout must be greater than 0"
  exit 1
end

Signal.trap("INT") do
  exit 2
end

config = BasicConfiguration.new
sqs = AwsContext.instance.sqs(*config.keys)
begin
  url = sqs.list_queues(name).first
  if url.nil? 
    puts "queue not found: #{name}"
  else  
    sqs.set_queue_attribute(url, visibility_timeout)
  end
rescue
  puts "error #{$!} #{name}"
  exit 1
end

